package mall.service;

import mall.core.jdbc.Page;
import mall.entity.Cart;
import mall.entity.Order;
import mall.entity.OrderItem;
import mall.repository.OrderDao;
import mall.repository.OrderItemDao;
import mall.repository.ProductDao;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class OrderService {

    private final CartService cartService = new CartService();
    private final OrderItemDao orderItemDao = new OrderItemDao();
    private final OrderDao orderDao = new OrderDao();
    private final ProductDao productDao = new ProductDao();

    public void create(Integer userId, String name, String address, String phone) {
        List<Cart> cartList = cartService.list(userId);

        String orderNumber = UUID.randomUUID().toString().replaceAll("-", "");


        BigDecimal totalPrice = BigDecimal.ZERO;

        for (Cart cart : cartList) {
            if (cart.getSelected() == 1) {
                BigDecimal itemPrice = cart.getProduct().getPrice();
                BigDecimal quantity = new BigDecimal(cart.getQuantity());
                totalPrice = totalPrice.add(itemPrice.multiply(quantity));


                OrderItem orderItem = new OrderItem();
                orderItem.setOrderNumber(orderNumber);
                orderItem.setProductId(cart.getProductId());
                orderItem.setPrice(cart.getProduct().getPrice());
                orderItem.setQuantity(cart.getQuantity());
                orderItemDao.insert(orderItem);

                cartService.remove(cart.getId());
            }
        }

        Order order = new Order();
        order.setNumber(orderNumber);
        order.setTotalPrice(totalPrice);
        order.setUserId(userId);
        order.setAddress(address);
        order.setPhone(phone);
        order.setName(name);
        orderDao.insert(order);

    }

    public List<Order> list(Integer userId) {
        List<Order> orderList = orderDao.selectListByUserId(userId);
        if (!orderList.isEmpty()) {
            return orderList.stream().map(order -> details(order.getId())).collect(Collectors.toList());
        }
        return orderList;
    }

    public List<Order> list(Order order) {
        List<Order> orderList = orderDao.selectList(order);
        if (!orderList.isEmpty()) {
            return orderList.stream().map(o -> details(o.getId())).collect(Collectors.toList());
        }
        return orderList;
    }

    public Page<Order> page(Page<Order> page, Order order) {
        Integer total = orderDao.count(order);
        List<Order> list = orderDao.selectList(order, page.getOffset(), page.getPageSize());
        List<Order> orderList = list.stream().map(o -> details(o.getId())).collect(Collectors.toList());
        page.setTotal(total);
        page.setList(orderList);
        return page;
    }

    public Order details(Integer orderId) {
        Order order = orderDao.selectById(orderId);
        List<OrderItem> orderItemList = orderItemDao.selectListByOrderNumber(order.getNumber());
        for (OrderItem item : orderItemList) {
            item.setProduct(productDao.selectById(item.getProductId()));
        }

        order.setOrderItemList(orderItemList);
        return order;
    }

    public int pay(Integer orderId) {
        return orderDao.updateStatus(orderId, 1);
    }

    public int cancel(Integer orderId) {
        return orderDao.updateStatus(orderId, 9);
    }
}
